package com.hq.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bus.domain.Rent;
import com.hq.bus.mapper.RentMapper;
import com.hq.bus.service.RentService;
import com.hq.bus.vo.RentVo;
import com.hq.sys.util.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsPackage: com.hq.bus.service.impl
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-18  09:50
 * @Description: TODO
 * @Version: 1.8
 */
@Service
@Transactional
public class RentServiceImpl implements RentService {
    @Autowired
    private RentMapper rentMapper;

    @Override
    public Rent queryRentByIdentity(String rentVo) {
        return rentMapper.selectByPrimaryKey(rentVo);

    }
    //根据时间段查询所有出租单信息
    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        // List<Customer> data = this.customerMapper.selectAll();
        Weekend<Rent> weekend = Weekend.of(Rent.class);
        if (Objects.nonNull(rentVo.getStartTime()) && Objects.nonNull(rentVo.getEndTime())){
            weekend.weekendCriteria().andBetween(Rent::getCreatetime, rentVo.getStartTime(), rentVo.getEndTime());
        }
        if (StrUtil.isNotBlank(rentVo.getIdentity())){
            weekend.weekendCriteria().andEqualTo(Rent::getIdentity, rentVo.getIdentity());
        }
        if (StrUtil.isNotBlank(rentVo.getRentid())){
            weekend.weekendCriteria().andLike(Rent::getRentid, "%" + rentVo.getRentid() + "%");
        }
        if(StrUtil.isNotBlank(rentVo.getIdentity())){
            weekend.weekendCriteria().andLike(Rent::getIdentity, "%" + rentVo.getIdentity() + "%");
        }
        if (Objects.nonNull(rentVo.getRentflag())){
            weekend.weekendCriteria().andEqualTo(Rent::getRentflag, rentVo.getRentflag());
        }
        List<Rent> data = rentMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public List<Rent> queryAllRents() {
        return rentMapper.selectAll();
    }

    @Override
    public int updateRent(RentVo rentVo) {
        return rentMapper.updateByPrimaryKey(rentVo);

    }
}
